import cv2  
import numpy as np  
from PIL import Image  
    
image_path = 'C:\\Users\\lenovo\\Desktop\\Math\\Trump.png'  
image = cv2.imread(image_path)  
    
if image is None:  
    raise ValueError("图像未成功读取")  
  
# 提取RGB通道,split默认按照BGR顺序拆分  
blue_channel, green_channel, red_channel = cv2.split(image)  
  
# 创建单独的通道分量图像  
blue_channel_image = cv2.merge((blue_channel, np.zeros_like(blue_channel), np.zeros_like(blue_channel)))  
green_channel_image = cv2.merge((np.zeros_like(green_channel), green_channel, np.zeros_like(green_channel)))  
red_channel_image = cv2.merge((np.zeros_like(red_channel), np.zeros_like(red_channel), red_channel))  
  
# 色彩加强处理（仅对红色和绿色通道）  
enhancement_factor_red = 2.0  # 红色增强系数  
enhancement_factor_green = 2.0  # 绿色增强系数  
  
red_channel_enhanced = np.clip(red_channel * enhancement_factor_red, 0, 255).astype(np.uint8)  
green_channel_enhanced = np.clip(green_channel * enhancement_factor_green, 0, 255).astype(np.uint8)  
  
# 蓝色通道不变，合并增强后的红色和绿色通道  
enhanced_red_image = cv2.merge((blue_channel, green_channel, red_channel_enhanced))  
enhanced_green_image = cv2.merge((blue_channel, green_channel_enhanced, red_channel))  
  
# 转换为PIL Image对象以便显示和保存  
def convert_and_save(image, filename):  
    pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))  
    pil_image.show()    
    pil_image.save(filename) 

output_dir = 'C:\\Users\\lenovo\\Desktop\\Math\\picture/'    
  
# 保存各个通道分量和增强后的图像  
convert_and_save(blue_channel_image, output_dir + 'B通道分量.jpg')  
convert_and_save(green_channel_image, output_dir + 'G通道分量.jpg')  
convert_and_save(red_channel_image, output_dir + 'R通道分量.jpg')  
convert_and_save(enhanced_red_image, output_dir + '红色加强.jpg')  
convert_and_save(enhanced_green_image, output_dir + '绿色加强.jpg')  
  
print("所有图像已保存。")
